package com.dh.jarvis.Util;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;

import com.dh.jarvis1.R;
import com.wang.avi.AVLoadingIndicatorView;

/**
 * Created by M.c on 2016/1/6.
 */
public class DialogUtil {

    private static DialogUtil mInstance;
    private AVLoadingIndicatorView loading;
    private static AlertDialog dialog;

    public static DialogUtil getInstance() {
        if (mInstance == null) {
            synchronized (DialogUtil.class) {
                if (mInstance == null) {
                    mInstance = new DialogUtil();
                }
            }
        }
        return mInstance;
    }

    private DialogUtil() {

    }

    public void showLoadingDialog(Context context) {
        if (null == dialog) {
            dialog = new AlertDialog.Builder(context).create();
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_loading, null);
            dialog.setView(view);
            loading = (AVLoadingIndicatorView) view.findViewById(R.id.loading);

            dialog.setCancelable(false);
//            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);//指定会全局,可以在后台弹出
            dialog.setCanceledOnTouchOutside(false);
        }
        loading.setVisibility(View.VISIBLE);

        try {
            if (!dialog.isShowing())
                dialog.show();

//            LogUtil.d("打开等待对话框");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void dismissLoadingDialog() {
        if (null != dialog && dialog.isShowing()) {
            loading.setVisibility(View.GONE);
            dialog.cancel();
//            LogUtil.d("关闭等待对话框");
        }
    }


    /**
     * 显示自定义View对话框
     *
     * @param context
     * @param view
     */
    public void showDialog(Context context, View view) {

        if (null == dialog) {
            dialog = new AlertDialog.Builder(context).create();
        }
        if (dialog.isShowing()) {
            return;
        }
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setView(view);
        dialog.show();

    }

    /**
     * 关闭自定义View对话框
     */
    public void dismissDialog() {
        if (null != dialog && dialog.isShowing()) {
            dialog.cancel();
        }
    }


}
